const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const Ymd = new Date()
exports.main = async(event, context) => {
  try {
    if (event.teaState == 0) {
      await db.collection('Topic').doc(event.topicId).update({
        data: {
          ktzt: 0,
          stuId: '',
          stuName: ''
        }
      })
      await db.collection('Student').doc(event.stuId).update({
        data: {
          stuState: 0,
          stuTopicId: '',
          stuTopicName: ''
        }
      })
      await db.collection('Message').doc(event.messageId).update({
        data: {
          dropState: 1
        }
      })
      await db.collection('Message').add({
        data: {
          sendId: event.teaId,
          sendName: event.teaName,
          receiveId: event.stuId,
          messageType: '退题成功',
          topicId: event.topicId,
          topicName: event.topicName,
          time: Date.parse(new Date()),
          dateYmd: Ymd.getFullYear() + '/' + (Ymd.getMonth() + 1) + '/' + Ymd.getDate()
        }
      })
    } else if (event.teaState == 1) {
      await db.collection('Topic').doc(event.topicId).update({
        data: {
          ktzt: 0,
          stuId: '',
          stuName: ''
        }
      })
      await db.collection('Student').doc(event.stuId).update({
        data: {
          stuState: 0,
          stuTopicId: '',
          stuTopicName: ''
        }
      })
      await db.collection('Message').doc(event.messageId).update({
        data: {
          dropState: 1
        }
      })
      await db.collection('Topic').where({
        ktzt: 2
      }).update({
        data: {
          ktzt: 0
        }
      })
      await db.collection('Teacher').doc(event.teaId).update({
        data: {
          teaState: 0
        }
      })
      await db.collection('Message').add({
        data: {
          sendId: event.teaId,
          sendName: event.teaName,
          receiveId: event.stuId,
          messageType: '退题成功',
          topicId: event.topicId,
          topicName: event.topicName,
          time: Date.parse(new Date()),
          dateYmd: Ymd.getFullYear() + '/' + (Ymd.getMonth() + 1) + '/' + Ymd.getDate()
        }
      })
    }
    return {
      result: 1
    }
  } catch (e) {
    return {
      result: 0
    }
  }
}